package com.ztstkj.lzscale.ui

import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.ToastUtils
import com.sscl.baselibrary.utils.DebugUtil
import com.ztstkj.lzscale.R
import com.ztstkj.lzscale.adpters.OptionAdapter
import com.ztstkj.lzscale.adpters.OptionItem
import com.ztstkj.lzscale.databinding.ActivitySettingBinding
import com.ztstkj.lzservices.mmkvs.MmkvManager
import com.ztstkj.lzservices.model.SettlementEnum
import com.ztstkj.lzservices.model.SysSetting
import com.ztstkj.lzservices.model.SysTypeEnum
import com.ztstkj.lzservices.utils.SpeakUtil

class SettingActivity : BaseActivity() {
    private lateinit var binding: ActivitySettingBinding
    private var currentSetting: SysSetting? = null
    private lateinit var sysTypeAdapter: OptionAdapter
    private lateinit var settlementAdapter: OptionAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_setting)
        binding.tvEquipmentRealNo.text = DeviceUtils.getAndroidID()
        bindTextViewEvent()
        initSysType()
        initSettlement()
        loadSysSetting()
    }

    private fun bindTextViewEvent() {
        binding.tvIp.text= NetworkUtils.getIPAddress(true)
        binding.btnSettingSave.setOnClickListener {
            currentSetting=SysSetting(sysTypeAdapter.selectedValue,settlementAdapter.selectedValue,binding.tvTakePhotoDelay.text.toString().toLong())
            MmkvManager.setSysSetting(currentSetting)
            ToastUtils.showLong("设置成功", 2000)
        }
        binding.imgBack.setOnClickListener {
            if(sysTypeAdapter.selectedValue== SysTypeEnum.COLLECT.value){
                startActivity(Intent(applicationContext, MainActivity::class.java))
                finish()
            }else if(sysTypeAdapter.selectedValue== SysTypeEnum.RECYCLABLE.value){
                startActivity(Intent(applicationContext, RecyclableActivity::class.java))
                finish()
            }else{
                SpeakUtil.speech(this,"请选择系统设置")
            }
        }
    }

    private fun loadSysSetting() {
        currentSetting = MmkvManager.getSysSetting()
        DebugUtil.debugOut("loadSysSetting: ${currentSetting!=null}")
        if (currentSetting != null) {
            sysTypeAdapter.setValue(currentSetting!!.sysType)
            settlementAdapter.setValue(currentSetting!!.settlement)
        }else{
            SpeakUtil.speech(this,"请选择系统设置")
        }
    }
    private fun initSysType(){
        val options=SysTypeEnum.entries.map { type-> OptionItem(type.value,type.desc) }
        //options.forEach { p-> println("${p.value},${p.name}") }
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        val recyclerview = binding.recyclerSysTypeOption
        recyclerview.layoutManager = linearLayoutManager
        sysTypeAdapter = OptionAdapter(options)
        recyclerview.adapter = sysTypeAdapter
    }
    private fun initSettlement(){
        val options= SettlementEnum.entries.map { type-> OptionItem(type.value,type.desc) }
        //options.forEach { p-> println("${p.value},${p.name}") }
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        val recyclerview = binding.recyclerSettlementOption
        recyclerview.layoutManager = linearLayoutManager
        settlementAdapter = OptionAdapter(options)
        recyclerview.adapter = settlementAdapter
    }

}